package com.disease.api.service.web.login;

import com.disease.api.service.config.JwtService;
import com.disease.api.service.entity.EmrPatientInfo;
import com.disease.api.service.mapper.EmrPatientInfoMapper;
import com.disease.api.service.model.LoginUser;
import com.disease.api.service.model.ResponseResult;
import com.disease.api.service.model.response.BaseUserResponse;
import com.disease.api.service.service.LoginService;
import com.disease.api.service.web.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/disease/v1/api/user")
public class UserController extends BaseController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private EmrPatientInfoMapper emrPatientInfoMapper;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<BaseUserResponse> login(@RequestBody LoginUser user) {
        return loginService.Login(user);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<LoginUser> loginOut(HttpServletRequest request) {
        String authToken = request.getHeader("Authorization");
        JwtService.logout(authToken);
        return ResponseResult.success();
    }


    @ResponseBody
    @RequestMapping(value = "/findPatient", method = RequestMethod.GET)
    public ResponseResult<List<EmrPatientInfo>> findUser(@RequestParam(value = "keyword") String key) {
        return ResponseResult.success(emrPatientInfoMapper.queryPatientByKey(key));
    }

}
